<?php

/**
 * This is the model class for table "account".
 *
 * The followings are the available columns in table 'account':
 * @property integer $identification
 * @property string $identity
 * @property string $password
 * @property string $authority
 * @property string $name
 * @property string $email
 * @property integer $is_female
 * @property string $phone
 * @property string $address
 * @property string $birth
 * @property integer $organization
 * @property string $language
 * @property string $note
 * @property string $insert
 * @property string $update
 * @property string $delete
 */
class Account extends CActiveRecord
{
    const REGISTER   = "register";
    const ADMIN      = "admin";
    const SUPERADMIN = "superAdmin";
    const HOST       = "host";

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Account the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'account';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('identity, password, name, email, is_female', 'required'),
            array('is_female, organization', 'numerical', 'integerOnly'=>true),
            array('identity, name, email', 'length', 'max'=>140),
            array('password, address', 'length', 'max'=>256),
            array('authority', 'length', 'max'=>12),
            array('phone', 'length', 'max'=>20),
            array('language', 'length', 'max'=>4),
            array('birth, note, insert, update, delete', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('identity, password, authority, name, email, is_female, phone, address, birth, organization', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'identification' => 'Key',
            'identity'       => 'Account',
            'password'       => 'Password',
            'authority'      => 'Authority',
            'name'           => 'Name',
            'email'          => 'Email',
            'is_female'      => 'Is Female',
            'phone'          => 'Phone',
            'address'        => 'Address',
            'birth'          => 'Birth',
            'organization'   => 'Organization',
            'language'       => 'Language',
            'note'           => 'Note',
            'insert'         => 'Insert',
            'update'         => 'Update',
            'delete'         => 'Delete',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('identity',$this->identity,true);
        $criteria->compare('password',$this->password,true);
        $criteria->compare('authority',$this->authority,true);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('email',$this->email,true);
        $criteria->compare('is_female',$this->is_female);
        $criteria->compare('phone',$this->phone,true);
        $criteria->compare('address',$this->address,true);
        $criteria->compare('birth',$this->birth,true);
        $criteria->compare('organization',$this->organization);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }


    /**
     * get list of account from database
     *
     * @author  TuanNguyen
     * @version Created 2013-08-15
     * @param   int    $i_page
     *                 current page
     * @param   int    $i_amountPerPage
     *                 amount of data in a page
     * @param   string $s_order
     *                 SQL string to order data
     * @param   string $s_where
     *                 SQL string to filter data
     * @return  array
     */
    public static function getDataList ($i_page, $i_amountPerPage, $s_order, $s_where)
    {
        //Call SQL procedure
        $command = Yii::app()->db->createCommand('CALL account_09_getList(:p_page,:p_amount,:p_oder,:p_where)');

        //Set In parameter into SQL statement
        $command->bindValue('p_page',   $i_page);
        $command->bindValue('p_amount', $i_amountPerPage);
        $command->bindValue('p_oder',   $s_order);
        $command->bindValue('p_where',  $s_where);

        $arr_model = array();
        $arr_data  = $command->queryAll();

        foreach ($arr_data as $key => $row) {
            $m_account = new Account();
            $m_account->unsetAttributes();

            $m_account->identification = $row["identification"];
            $m_account->identity = $row["identity"];
            $m_account->password = $row["password"];
            $m_account->authority = $row["authority"];
            $m_account->name = $row["name"];
            $m_account->email = $row["email"];
            $m_account->is_female = $row["is_female"];
            $m_account->phone = $row["phone"];
            $m_account->address = $row["address"];
            $m_account->birth = $row["birth"];
            $m_account->organization = $row["organization"];
            $m_account->language = $row["language"];
            $m_account->note = $row["note"];
            $m_account->insert = $row["insert"];
            $m_account->update = $row["update"];
            $m_account->delete = $row["delete"];

            array_push($arr_model, $m_account);
        } // End foreach

        return $arr_model;
    } // End getDataList


}